package com.springboot.lotterysystem.common.pojo;

import com.springboot.lotterysystem.common.errorcode.ErrorCode;
import com.springboot.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * Description :
 * User: jiume
 * Date: 2025-04-19
 * Time: 18:43
 */
@Data
public class CommonResult<T> implements Serializable { // 标记为可序列化类
    // 响应码
    private Integer code;
    // 响应信息
    private String msg;
    // 响应数据
    private T data;

    public static <T>CommonResult<T> success(T data) {
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(GlobalErrorCodeConstants.SUCCESS.getCode());
        commonResult.setMsg("");
        commonResult.setData(data);
        return commonResult;
    }

    public static <T>CommonResult<T> error(Integer code, String msg) {
        // 首先使用断言去判断code是否符合error的情况
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.equals(code), "code 不是错误的异常");
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(code);
        commonResult.setMsg(msg);
        return commonResult;
    }

    public static <T>CommonResult<T> error(ErrorCode errorCode) {
        /*CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(errorCode.getCode());
        commonResult.setMsg(errorCode.getMsg());
        return commonResult;*/

        // 上述代码可以简化为下面的形式
        return error(errorCode.getCode(), errorCode.getMsg());
    }

}
